package com.controller;

import com.mapper.DeptMapper;
import com.pojo.Dept;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
public class DeptController {

    @Resource
    public DeptMapper deptMapper;
    //单个查询
    @RequestMapping("/selectDeptById.do")
    public String selectDept(Dept dept, Map map){
            Example e = new Example(Dept.class);
//        但是null代表堆内存中根本没有这个东西，
//         而空字符串就完全不同了，
//         空字符串也是字符串，他是有内存空间的
            //?
            e.createCriteria().andEqualTo("bumentableid",dept.getBumentableid());
            List<Dept> deptList = deptMapper.selectByExample(e);
            map.put("bumenTableid",deptList.get(0).getBumentableid());
            map.put("deptList",deptList);
            return "deptList";
    }

    //查询所有dept记录
    @RequestMapping("/selectAllDept")
    public String selectAllDept(Map map){
        List<Dept> deptList = deptMapper.selectAll();
        map.put("deptList",deptList);
        return "deptList";
    }

    //添加
    @RequestMapping("/add")
    public String add(Dept dept,Map map){
        int i = deptMapper.insert(dept);
        if(i>0){
            map.put("msg","alert('添加成功！！！')");
        }else{
            map.put("msg","alert('添加失败！！！')");
        }
        return "forward:/selectAllDept";
    }

    @RequestMapping("/deleteById")
    public String deleteDept(Dept dept,Map map){
        System.out.println(dept);
        Example e = new Example(Dept.class);
        if(dept!=null){
            e.createCriteria().andEqualTo("bumentableid",dept);
        }
        int i = deptMapper.deleteByPrimaryKey(dept);
        if(i>0){
            map.put("msg","alert('删除成功！！！')");
        }else{
            map.put("msg","alert('删除失败！！！')");
        }
        return "forward:/selectAllDept";
    }

    //修改查询
    @RequestMapping("/selectForedit")
    public String selectDeptById(Dept dept, Map map){
        System.out.println(dept);
        Example e = new Example(Dept.class);
        e.createCriteria().andEqualTo("bumentableid",dept.getBumentableid());
        List<Dept> deptList = deptMapper.selectByExample(e);
        map.put("deptList",deptList);
        return "/部门表CURD/editDept";
    }

    @RequestMapping("/editDept")
    public String edit(Dept dept,Map map){
        System.out.println(dept);
        Example e = new Example(Dept.class);
        if(dept!=null){
            e.createCriteria().andEqualTo("bumentableid",dept);
        }
//        int i = deptMapper.updateByExampleSelective(dept, e);
        int i = deptMapper.updateByPrimaryKey(dept);
        System.out.println("自增主键为："+dept.getBumentableid());
        if(i>0){
            map.put("msg","alert('修改成功！！！')");
        }else{
            map.put("msg","alert('修改失败！！！')");
        }
        return "forward:/selectAllDept";
    }

    //分页
//    @RequestMapping("/page.do")
//    pub
}
